package com.duolaibei.business.view.service;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.igexin.sdk.GTServiceManager;

/**
 * Created by xlm on 2017/8/8 0008
 */

public class DlbPushService extends Service {
    private final static int GRAY_SERVICE_ID = 1001;
    private static Notification notification;

    @Override
    public void onCreate() {
        super.onCreate();
        notification = new Notification();
        notification.flags = Notification.FLAG_ONGOING_EVENT;
        notification.flags |= Notification.FLAG_NO_CLEAR;
        notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
        GTServiceManager.getInstance().onCreate(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        flags = START_STICKY;
        //启用前台服务，主要是startForeground()
        if (Build.VERSION.SDK_INT < 18) {
            //API < 18，启动前台Service时直接传入new Notification()
            startForeground(GRAY_SERVICE_ID, notification);//API < 18
        } else {
            //API >= 18，同时启动两个id相同的前台Service，然后再将后启动的Service做stop处理；
            Intent innerIntent = new Intent(this, GrayInnerService.class);
            startService(innerIntent);
            startForeground(GRAY_SERVICE_ID, notification);
        }
//        AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
//        //读者可以修改此处的Minutes从而改变提醒间隔时间
//        //此处是设置每隔15分钟启动一次
//        //这是5分钟的毫秒数
//        int Minutes = 15 * 60 * 1000;
//        //SystemClock.elapsedRealtime()表示1970年1月1日0点至今所经历的时间
//        long triggerAtTime = SystemClock.elapsedRealtime() + Minutes;
//        //此处设置开启AlarmReceiver这个Service
//        Intent i = new Intent(this, Alarmreceiver.class);
//        PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
//        //ELAPSED_REALTIME_WAKEUP表示让定时任务的出发时间从系统开机算起，并且会唤醒CPU。
//        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
        return GTServiceManager.getInstance().onStartCommand(this, intent, flags, startId);
    }

    /**
     * 给 API >= 18 的平台上用的灰色保活手段
     */
    public static class GrayInnerService extends Service {

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            startForeground(GRAY_SERVICE_ID, notification);
            stopForeground(true);
            stopSelf();
            return super.onStartCommand(intent, flags, startId);
        }

        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

    }

    @Override
    public IBinder onBind(Intent intent) {
        return GTServiceManager.getInstance().onBind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopForeground(true);
        GTServiceManager.getInstance().onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        GTServiceManager.getInstance().onLowMemory();
    }

}
